package com.dcm.bargain.service.impl;

import com.dcm.bargain.entity.CutPriceEntity;
import com.dcm.bargain.mapper.RecordMapper;
import com.dcm.bargain.service.RecordService;
import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Mengxiangjian
 * @Date: 2021/4/1 10:04
 * @Version V 1.0
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<CutPriceEntity> findAll(Long uid) {
        checkRecord(uid);
        return recordMapper.findAll(uid);
    }

    /**
     * 查询帮砍的好友
     * @return
     */
    @Override
    public List<String> findHelper(Long uid,Long bid) {
        checkRecord(uid);
        ArrayList<String> strings = new ArrayList<>();
        List<String> helper = recordMapper.findHelper(uid,bid);
        String s = helper.get(0);
        String s2 = helper.get(1);
        String[] split1 = s.split("-");
        String[] split2 = s2.split("-");
        for (String s1 : split1) {
            strings.add(s1);
        }
        for (String s1 : split2) {
            strings.add(s1);
        }
        strings.subList(0,1).clear();
        strings.subList(4,5).clear();
        return strings;
    }

    /**
     * 倒叙
     * @return
     */
    @Override
    public List<CutPriceEntity> findAllDesc() {
        return recordMapper.findAllDesc();
    }

    /**
     * 发货
     * @return
     */
    @Override
    public List<CutPriceEntity> findAllDpt() {
        return recordMapper.findAllDpt();
    }

    /**
     * 静态调用方法
     * @param uid
     */
    public static void checkRecord(Long uid){
        if (uid==null){
            throw new MyException(Constants.OrderErrorMsg.ORDER_UID_CANNOT_NULL);
        }
    }
}
